package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
	@Autowired
	private SetmealMapper setmealMapper;
	@Autowired
	private SetmealDishMapper setmealDishMapper;

	@Override
	@Transactional
	public void save(SetmealDTO setmealDTO) {
		//准备数据
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealDTO, setmeal);
		//向套餐表添加一条数据
		setmealMapper.insert(setmeal);

		//获取insert生成的主键值
		Long SetmealId = setmeal.getId();

		//向套餐关系表添加n条数据
		//获得实体对象
		List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();
		if(setmealDishs!=null&& setmealDishs.size()>0){}
		setmealDishs.forEach(setmealDish -> {
			setmealDish.setSetmealId(SetmealId);
		});
		setmealDishMapper.insert(setmealDishs);
	}

	/**
	 * 根据条件分页查询套餐
	 *
	 * @param setmealPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
		PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
		Page<SetmealVO> page = setmealMapper.find(setmealPageQueryDTO);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 批量删除菜品
	 * @param ids
	 */
	@Override
	@Transactional
	public void delete(List<Long> ids) {
		//删除套餐
		setmealMapper.delete(ids);
		//删除套餐和菜品对应关系
		setmealDishMapper.delete(ids);
	}

	/**
	 * 根据id查询套餐和套餐菜品关系
	 * @param id
	 * @return
	 */
	@Override
	public SetmealVO getById(Long id) {
	//获取套餐数据
	Setmeal setmeal=setmealMapper.getById(id);
	//获取套餐菜品关系数据
	List<SetmealDish> setmealDishs =setmealDishMapper.getBySetmealId(id);
	//封装到SetmealVO中
		SetmealVO setmealVO=new SetmealVO();
		BeanUtils.copyProperties(setmeal,setmealVO);
		setmealVO.setSetmealDishes(setmealDishs);
	return setmealVO;
	}

	/**
	 * 根据id修改套餐和套餐菜品关系表
	 * @param setmealDTO
	 */
	@Override
	public void update(SetmealDTO setmealDTO) {
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealDTO, setmeal);
		//修改套餐
		setmealMapper.update(setmeal);

		//修改套餐菜品关系表
		//获取套餐id,封装到集合中
		List<Long> setmealId=new ArrayList<Long>();
		setmealId.add(setmeal.getId());
		//删除原有关系数据
		setmealDishMapper.delete(setmealId);
		//插入
		//获取套餐和菜品关系集合
		List<SetmealDish> setmealDishs=setmealDTO.getSetmealDishes();
		//判断集合是否为空
		if(setmealDishs!=null&& setmealDishs.size()>0){
			//遍历集合设置套餐id
			setmealDishs.forEach(setmealDish -> {
				setmealDish.setSetmealId(setmealDTO.getId());
			});
			//向关系表重新插入数据
			setmealDishMapper.insert(setmealDishs);
		}
	}

	/**
	 * 根据id起售和停售套餐
	 * @param status
	 * @param id
	 */
	@Override
	public void startOrstop(Integer status, Long id) {
		setmealMapper.updateStatus(status,id);
	}

	/**
	 * 条件查询
	 * @param setmeal
	 * @return
	 */
	public List<Setmeal> list(Setmeal setmeal) {
		List<Setmeal> list = setmealMapper.list(setmeal);
		return list;
	}

	/**
	 * 根据id查询菜品选项
	 * @param id
	 * @return
	 */
	public List<DishItemVO> getDishItemById(Long id) {
		return setmealMapper.getDishItemBySetmealId(id);
	}
}

